//SUMMARY:  This do file cleans data from original survey for analysis presented in Ballard-Rosa, Jensen & Scheve (2021)

clear all
set more off

//Set directory
cd "C:\Users\cambr\Dropbox\US Authoritarianism\Paper\ISQ\ISQ replication materials\Data\"

*** Load data
use STAN0105_OUTPUT, clear

foreach x of varlist _all {
capture rename `x' `=lower("`x'")'
}

gen freqweight=round(weight*10)

*** Rename variables and make value labels
rename q1_treat satisfied_scale_direction
g satisfaction_us_economy=q1
replace satisfaction_us_economy=10 if q1b==1
replace satisfaction_us_economy=9 if q1b==2
replace satisfaction_us_economy=8 if q1b==3
replace satisfaction_us_economy=7 if q1b==4
replace satisfaction_us_economy=6 if q1b==5
replace satisfaction_us_economy=5 if q1b==6
replace satisfaction_us_economy=4 if q1b==7
replace satisfaction_us_economy=3 if q1b==8
replace satisfaction_us_economy=2 if q1b==9
replace satisfaction_us_economy=1 if q1b==10
drop q1 q1b

lab def satisfaction 1 "Very satisfied" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "Very dissatisfied"
lab val satisfaction_us_economy satisfaction

* Submission
rename q2a believe_leaders
rename q2b leaders_know_best
rename q2c criticize_authorities_rev
recode criticize_authorities_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q2d authorities_truthful
rename q2e skeptical_authorities_rev
recode skeptical_authorities_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q2f questioning_healthy_rev
recode questioning_healthy_rev (1=5) (2=4) (3=3) (4=2) (5=1)

gen avg_submission = (believe_leaders + leaders_know_best + criticize_authorities_rev + authorities_truthful + skeptical_authorities_rev + questioning_healthy_rev) / 6
alpha believe_leaders leaders_know_best criticize_authorities_rev authorities_truthful skeptical_authorities_rev questioning_healthy_rev, item

* Conventionalism
rename q2g too_much_tradition_rev
recode too_much_tradition_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q2h traditions_foundation_society
rename q2i follow_social_traditions
rename q2j traditions_interf_progress_rev
recode traditions_interf_progress_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q2k challenge_social_traditions_rev
recode challenge_social_traditions_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q2l respect_norms

gen avg_conventionalism = (too_much_tradition_rev + traditions_foundation_society + follow_social_traditions + traditions_interf_progress_rev + challenge_social_traditions_rev + respect_norms) / 6
alpha too_much_tradition_rev traditions_foundation_society follow_social_traditions traditions_interf_progress_rev challenge_social_traditions_rev respect_norms, item

* Aggression
rename q2m force_necessary_groups_threat
rename q2n force_necessary_indv_threat
rename q2o police_avoid_violence_rev
recode police_avoid_violence_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q2p people_avoid_violence_rev
recode people_avoid_violence_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q2q force_wrong_rev
recode force_wrong_rev (1=5) (2=4) (3=3) (4=2) (5=1)
rename q2r strong_punishments_necessary

g avg_aggression = (force_necessary_groups_threat + force_necessary_indv_threat + police_avoid_violence_rev + people_avoid_violence_rev + force_wrong_rev + strong_punishments_necessary) / 6
alpha force_necessary_groups_threat force_necessary_indv_threat police_avoid_violence_rev people_avoid_violence_rev force_wrong_rev strong_punishments_necessary, item

gen asc = (avg_aggression + avg_submission + avg_conventionalism) / 3

* Equal treatment fairness experiment
rename q3_treat tax_treatment
lab var tax_treatment "Tax Preferences Treatment"
lab def treatment 1 "Voting Rights Treatment" 2 "Thanksgiving Treatment"
lab val tax_treatment treatment 
rename q3a equal_voting_rights
rename q4aa voting_rights_reason1
label var voting_rights_reason1 "Equal Voting Rights Reason 1"
rename q4ab voting_rights_reason2
label var voting_rights_reason2 "Equal Voting Rights Reason 2"
rename q4ac voting_rights_reason3
label var voting_rights_reason3 "Equal Voting Rights Reason 3"
rename q3c thanksgiving
rename q4ca thanksgiving_reason1
label var thanksgiving_reason1 "Thanksgiving Reason 1"
rename q4cb thanksgiving_reason2
label var thanksgiving_reason2 "Thanksgiving Reason 2"
rename q4cc thanksgiving_reason3
label var thanksgiving_reason3 "Thanksgiving Reason 3"
rename q5 treat_citizens_equally
rename q6 high_inc_tax_share
g much_somewhat_inc=q6_larger if q6_larger!=.
replace much_somewhat_inc=q6_smaller if q6_smaller!=.
drop q6_larger q6_smaller
lab def muchsomewhat 1 "Much" 2 "Somewhat"
lab val much_somewhat_inc muchsomewhat

* Policy and beliefs
rename q7 success
g effort=(success==1)
rename q8 gov_services
rename q9 degree_change_services
rename q10 diff_top10_bot50
g smaller_inc_dif=(diff_top10_bot50==1)
rename q11 degree_inc_diff
rename q12 other_ethnics
g other_ethnics_favored=(other_ethnics==1 | other_ethnics==2)
rename q13 death_penalty
g favor_death_penalty=(death_penalty==1)

rename q14 police_strike
g accept_police_strike=(police_strike==1)
rename q15 police_strike_said_things
g accept_strike_said_things=(police_strike_said_things==1)
rename q16 police_strike_murder
g accept_strike_murder=(police_strike_murder==1)
rename q17 police_strike_custody
g accept_strike_custody=(police_strike_custody==1)
rename q18 police_strike_fist
g accept_strike_fist=(police_strike_fist==1)
rename q19 public_officials
g public_officials_dont_care=(public_officials==1 | public_officials==2)
rename q20 have_a_say
g dont_have_a_say=(have_a_say==1 | have_a_say==2)

rename q21a respect_v_indep
recode respect_v_indep (1=0) (2=1)
label define cr1 0 "Independence" 1 "Respect"
lab val respect_v_indep cr1
rename q21b manners_v_curiosity
recode manners_v_curiosity (1=0) (2=1)
label define cr2 0 "Curiosity" 1 "Good manners"
lab val manners_v_curiosity cr2
rename q21c obedience_v_self
recode obedience_v_self (1=1) (2=0)
label define cr3 0 "Self-reliance" 1 "Obedience"
lab val obedience_v_self cr3
rename q21d behaved_v_considerate
recode behaved_v_considerate (1=0) (2=1)
label define cr4 0 "Considerate" 1 "Behaved"
lab val behaved_v_considerate cr4
g child_rearing_auth = (respect_v_indep + manners_v_curiosity + obedience_v_self + behaved_v_considerate)/4
rename q22 american
g proud_american=(american==1 | american==2)
g very_proud_american=(american==1)
rename q23 law
g obey_law=(law==1)

* Anxiety
rename q24_1 nervous_anxious
rename q24_2 not_stop_worrying
rename q24_3 worry_too_much
rename q24_4 trouble_relaxing
rename q24_5 hard_sit_still
rename q24_6 easily_annoyed
rename q24_7 awful_might_happen
rename q25 anxiety_impact
replace anxiety_impact=0 if anxiety_impact==.
gen gad7 = nervous_anxious + not_stop_worrying + worry_too_much + trouble_relaxing + hard_sit_still + easily_annoyed + awful_might_happen

* Economic situation, trade, migration
rename q26 local_econ
g local_econ_improved=(local_econ==1 | local_econ==2)
rename q27 family_wellbeing
g family_wellb_improved=(family_wellbeing==1 | family_wellbeing==2)
rename q30 children_situation
g children_better=(children_situation==1| children_situation==2)
rename q31 parents_situation
g better_than_parents=(parents_situation==1)
rename q32 trade
g reduce_trade=(trade==4 | trade==5) // Reduce or keep at current level. Note: large "not sure" group of 20 %
rename q33 trade_effect
g trade_hurt_US=(trade_effect==4 | trade_effect==5)
rename q34 trade_effect_local
g trade_hurt_local=(trade_effect_local==4 | trade_effect_local==5)
rename q35 foreign_inv
g restrict_foreign_inv=(foreign_inv==2)
rename q36 foreign_inv_effect
g foreign_inv_bad=(foreign_inv_effect>=1 & foreign_inv_effect<=3)
rename q37 num_immigrants
g reduce_num_immigrants=(num_immigrants==4 | num_immigrants==5)
rename q38 immigrants_effect
g immigrants_bad=(immigrants_effect>=1 & immigrants_effect<=3)
g color_pink=(q39m_1==1)
g color_red=(q39m_2==1)
g color_green=(q39m_3==1)
g color_white=(q39m_4==1)
g color_black=(q39m_5==1)
g color_blue=(q39m_6==1) 
g colors_correct=(color_red==1 & color_green==1 & color_pink==0 & color_white==0 & color_black==0 & color_blue==0)
drop q39m_1-q39m_6
rename q40 left_right
rename q42 turnout
g did_vote=(turnout==2)
rename q43 vote
g clinton=(vote==1) if vote!=.
g trump=(vote==2) if vote!=.
g other_candidate=(vote==3) if vote!=.
rename q43 vote_other_txt
rename q28 unemployment_risk
g unemp_unlikely=(unemployment_risk==1 | unemployment_risk==2) if unemployment_risk!=.
rename q29 new_job
g difficult_new_job=(new_job==1 | new_job==2) if new_job!=.
rename q61 competition
g agree_competition=(competition==1 | competition==2 | competition==3) if competition!=. // Agree or neither agree nor disagree
rename q62 job_overseas
g worried_overseas=(job_overseas==1 | job_overseas==2 | job_overseas==3) if job_overseas!=.
rename q63 technology_threat
g worried_tech=(technology_threat==1 | technology_threat==2 | technology_threat==3) if technology_threat!=.
rename pid3 party_id
rename pid7 party_strong_leaning
g party_id_narrow=1 if party_id==1
replace party_id_narrow=2 if party_id==2
replace party_id_narrow=3 if party_id==3 | party_id==4 | party_id==5
g party_id_broad=1 if party_strong_leaning==1 | party_strong_leaning==2 | party_strong_leaning==3
replace party_id_broad=2 if party_strong_leaning==5 | party_strong_leaning==6 | party_strong_leaning==7
replace party_id_broad=3 if party_strong_leaning==4 | party_strong_leaning==8 | party_strong_leaning==9
lab define party 1 "Democrat" 2 "Replublican" 3 "Independent or Other"
lab val party_id_narrow party
lab val party_id_broad party
lab var party_id_narrow "Party ID (narrow definition)"
lab var party_id_broad "Party ID (broad definition)"
g democrat_narrow=(party_id_narrow==1) & party_id_narrow!=.
g republican_narrow=(party_id_narrow==2) & party_id_narrow!=.
g democrat_broad=(party_id_broad==1) & party_id_broad!=.
g republican_broad=(party_id_broad==2) & party_id_broad!=.
lab var democrat_narrow "Democrat (narrow definition)"
lab var republican_narrow "Republican (narrow definition)"
lab var democrat_broad "Democrat (broad definition)"
lab var republican_broad "Republican (broad definition)"

* State and county
* Check for only one county per respondent
foreach x of varlist county_al-county_wy {
g `x'_dummy=(`x'!=.)
}
egen check=rowtotal(county_al_dummy-county_wy_dummy), m
tab check // No one has more than one county; DC has no counties (5 observations)
drop check county_al_dummy-county_wy_dummy
* Make a string version of the county variable (right now it's nummeric and state+county number is unique)
foreach x of varlist county_al-county_wy {
decode `x', g(`x'_str)
}
* Make one county variable instead of current by-state structure (string and nummeric version)
g county=.
foreach x of varlist county_al-county_wy {
replace county=`x' if county==. & `x'!=.
}
drop county_al-county_wy
g county_str=""
foreach x of varlist county_al_str-county_wy_str {
replace county_str=`x' if county_str=="" & `x'!=""
}
drop county_al_str-county_wy_str
* Make string versions of states too
rename inputstate state
decode state, g(state_str)

* DC
replace county=1 if state==11
replace county_str="District of Columbia" if state==11

* Make unique county id (use this when merging to trade data)
g temp=state*1000
g countyid2010=temp+county
drop temp

* Demographics
rename birthyr birthyear
g age=2017-birthyear
gen age18_29 = (age >= 18 & age < 30)
gen age30_49 = (age >= 30 & age < 50)
gen age50_64 = (age >= 50 & age < 65)
gen agegt65 = (age >= 65)
rename q46 zipcode
rename q47 moving
recode moving (1=1) (2=0) 
lab def noyes 0 "No" 1 "Yes"
lab val moving noyes
rename q48 moves_outside
recode moves_outside (1=1) (2=0)
lab val moves_outside noyes
g stayed_same_area=(moving==0 | moving==1 & moves_outside==0)
rename q50 gender
g female=(gender==2)
rename q51 educ
g university_educ=(educ>=7 & educ<=10)
g above_hs_educ=(educ>=5 & educ<=6)
rename q52 marital_status
g couple=(marital_status==1 | marital_status==2 | marital_status==7)
rename q53 num_children
g children=(num_children!=0)
rename q55 prim_earner
g father_prim_earner=(prim_earner==2)
rename q55_t prim_earner_txt
rename q56 father_educ
g father_uni_educ=(father_educ>=7 & father_educ<=10)
rename q57 mother_educ
g mother_uni_educ=(mother_educ>=7 & mother_educ<=10)
rename q58 empl_status
g employed=(empl_status>=1 & empl_status<=3)
rename q59 spouse_empl_status
g spouse_employed=(spouse_empl_status>=1 & spouse_empl_status<=3) if spouse_empl_status!=.
rename q60 employer_name_txt
rename q64 income_individual
rename q65 income_household
rename q66 religion
rename q67 religion_importance
g religion_important=(religion_importance==1 | religion_importance==2)
rename q68 church
g church_often=(church==1 | church==2 | church==3 | church==4)
rename q69 survey_rating
rename q70 survey_comments_txt
g white=(race==1)
lab var white "White"

*Want to generate "region" dummies:
gen region = ""
replace region = "Northeast" if state_str == "Connecticut" | state_str == "Maine" | state_str == "Massachusetts" | state_str == "New Hampshire" ///
 | state_str == "Rhode Island" | state_str == "Vermont" | state_str == "New Jersey" | state_str == "New York" | state_str == "Pennsylvania"
replace region = "Midwest" if state_str == "Illinois" | state_str == "Indiana" | state_str == "Michigan" | state_str == "Ohio" | state_str == "Wisconsin" ///
 | state_str == "Iowa" | state_str == "Kansas" | state_str == "Minnesota" | state_str == "Missouri" | state_str == "Nebraska" | state_str == "North Dakota" | state_str == "South Dakota"
replace region = "South" if state_str == "Delaware" | state_str == "Florida" | state_str == "Georgia" | state_str == "Maryland" | state_str == "North Carolina" ///
 | state_str == "South Carolina" | state_str == "Virginia" | state_str == "District of Columbia" | state_str == "West Virginia" | state_str == "Alabama" ///
 | state_str == "Kentucky" | state_str == "Mississippi" | state_str == "Tennessee" | state_str == "Arkansas" | state_str == "Louisiana" | state_str == "Oklahoma" | state_str == "Texas"
replace region = "West" if state_str == "Arizona" | state_str == "Colorado" | state_str == "Idaho" | state_str == "Montana" | state_str == "Nevada" | state_str == "New Mexico" ///
 | state_str == "Utah" | state_str == "Wyoming" | state_str == "Alaska" | state_str == "California" | state_str == "Hawaii" | state_str == "Oregon" | state_str == "Washington"
encode region, gen(regionFE)

* Labels for new variables
lab var satisfaction_us_economy "Satisfaction with the US economy"
lab var avg_submission "Average submission" 
lab var avg_conventionalism "Average conventionalism"
lab var avg_aggression "Average agression"
lab var asc "Average ASC"
lab var much_somewhat_inc "Much/Somewhat change in top tax" 
lab var effort "Success comes from hard work"
lab var smaller_inc_dif "Top-bottom inc diff should be smaller"
lab var other_ethnics_favored "Other ethnic groups are favored"
lab var favor_death_penalty "In favor of the death penalty"
lab var accept_police_strike "Accept policeman striking a citizen"
lab var accept_strike_said_things "Accept policeman striking a citizen, if said things"
lab var accept_strike_murder "Accept policeman striking a citizen, if murder case"
lab var accept_strike_custody "Accept policeman striking a citizen, if escape custody"
lab var accept_strike_fist "Accept policeman striking a citizen, if attack with fists"
lab var public_officials_dont_care "Public officials don't care what people like me think"
lab var dont_have_a_say "I don't have a say about what government does"
lab var child_rearing_auth "Child rearing authority"
lab var proud_american "Proud or very proud to be American"
lab var very_proud_american "Very proud to be American"
lab var obey_law "People should obey the law without exception"
lab var gad7 "GAD 7 personality traits"
lab var local_econ_improved "Economy in community improved over the past decade"
lab var family_wellb_improved "Family's economic wellbeing improved over the past decade"
lab var children_better "Children will have better econ and social prospects"
lab var better_than_parents "Better off financially than my parents at same age"
lab var reduce_trade "Trade should be reduced or kept at current level"
lab var trade_hurt_US "Trade has hurt or neither hurt nor helped"
lab var trade_hurt_local "Trade has hurt or neither hurt nor helped locally"
lab var restrict_foreign_inv "Additional foreign investment should be restricted"
lab var foreign_inv_bad "Foreign investment is bad, response 1-4"
lab var reduce_num_immigrants "Number of immigrants should be reduced"
lab var immigrants_bad "Immigration is bad, response 1-4"
lab var color_pink "Chose color pink"
lab var color_red "Chose color red"
lab var color_green "Chose color green"
lab var color_white "Chose color white"
lab var color_black "Chose color black"
lab var color_blue "Chose color blue"
lab var colors_correct "Correctly chose red and green - passed attention test"
lab var did_vote "Voted at last election"
lab var clinton "Voted for Hillary Clinton at last election"
lab var trump "Voted for Donald Trump at last election"
lab var other_candidate "Voted for other candidate at last election"
lab var unemp_unlikely "Unlikely to become unemployed"
lab var difficult_new_job "Would be difficult to find a new job"
lab var agree_competition "Agree that own industry faces competition"
lab var worried_overseas "Worried that job is sent overseas"
lab var worried_tech "Worried that job is eliminated by technology"
lab var county "County id"
lab var county_str "County name"
lab var state_str "State name"
lab var countyid2010 "State-county id"
lab var age "Age"
lab var stayed_same_area "Has stayed in the same area in the past 20 years"
lab var female "Female"
lab var university_educ "University degree" 
lab var above_hs_educ "Some college or more education"
lab var couple "Couple, married or domestic partnership"
lab var children "Has children below age 18"
lab var father_prim_earner "Father was the primary earner"
lab var father_uni_educ "Father has university degree"
lab var mother_uni_educ "Mother has university degree"
lab var employed "In employment"
lab var spouse_employed "Spouse in employment"
lab var religion_important "Religion is important"
lab var church_often "Often attend church"


* Final data
save us_survey, replace 

disp "RUN NEXT:  Merge geographic data with survey.do"
